home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #2 / Monster Media No. 2 (Monster Media)(1994).ISO / prog_c / pclc41.zip / XYMODEM.C < prev    next >
Text File  |  1994-02-12  |  7KB  |  229 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <fcntl.h>
  5. #include <io.h>
  6. #include <sys\types.h>
  7. #include <sys\stat.h>
  8.  
  9. #include "pcl4c.h"
  10. #include "ascii.h"
  11. #include "term_io.h"
  12. #include "xypacket.h"
  13. #include "xymodem.h"
  14.  
  15. #define FALSE 0
  16. #define TRUE !FALSE
  17.  
  18.  
  19. int TxyModem(
  20.    int Port,            /* COM port [0..3] */
  21.    char Filename[],     /* filename buffer */
  22.    char Buffer[],       /* 1024 byte data buffer */
  23.    int OneKflag,        /* if TRUE, use 1K blocks when possible */
  24.    int BatchFlag)       /* if TRUE, send filename in packet 0 */
  25. {int i, k;
  26.  int Code;
  27.  int Handle;         /* file Handle */
  28.  char c;
  29.  int p;
  30.  char PacketType;
  31.  char PacketNbr;
  32.  int  PacketSize;
  33.  int  FirstPacket;
  34.  unsigned short CheckSum;
  35.  int Number1K = 0;       /* total # 1K packets */
  36.  int Number128 = 0;      /* total # 128 byte packets */
  37.  char NCGchar = NAK;
  38.  long FileSize;
  39.  char temp[81];
  40.  int EmptyFlag = FALSE;
  41.  /* begin */
  42.  if(BatchFlag) if(Filename[0]=='\0') EmptyFlag = TRUE;
  43.  if(!EmptyFlag)
  44.      {/* Filename is not empty */
  45.       EmptyFlag = FALSE;
  46.       Handle = open(Filename,O_RDONLY|O_BINARY,S_IREAD);
  47.       if(Handle<0)
  48.           {strcpy(temp,"Cannot open ");
  49.            strcat(temp,Filename);
  50.            DisplayLine(temp,NULL,0);
  51.            return(FALSE);
  52.           }
  53.      }
  54.  DisplayLine("XYMODEM send: waiting for Receiver ",NULL,0);
  55.  while(SioKeyPress()) SioKeyRead();
  56.  /* compute # blocks */
  57.  if(!EmptyFlag)
  58.      {FileSize = filelength(Handle);
  59.       if(OneKflag) Number1K = (int) (FileSize / 1024L);
  60.       Number128 = (int) ((FileSize-1024L*(long)Number1K) / 128L);
  61.       if(128L*Number128+1024*Number1K < FileSize) Number128++;
  62.       sprintf(temp,"%d 1024 & %d 128 byte packets",Number1K,Number128);
  63.       DisplayLine(temp,NULL,0);
  64.      }
  65.  else
  66.      {/* empty file */
  67.       Number128 = 0;
  68.       Number1K = 0;
  69.       /*DisplayLine("Empty File",NULL,0);*/
  70.      }
  71.  /* clear comm port ( there may be several NAKs queued up ) */
  72.  SioRxFlush(Port);
  73.  /* get receivers start up NAK, 'C', or 'G' */
  74.  if(!TxStartup(Port,&NCGchar)) return(FALSE);
  75.  /* loop over all packets */
  76.  if(BatchFlag) FirstPacket = 0;
  77.  else FirstPacket = 1;
  78.  for(p=FirstPacket;p<=Number1K+Number128;p++)
  79.        {/* user aborts ? */
  80.         if(SioKeyPress()) if((char)SioKeyRead()==CAN)
  81.           {TxCAN(Port);
  82.            DisplayLine("*** Canceled by USER ***",NULL,0);
  83.            return(FALSE);
  84.           }
  85.         /* issue message */
  86.         sprintf(temp,"Packet %d",p);
  87.         DisplayLine(temp,NULL,0);
  88.         /* load up Buffer */
  89.         if(p==0)
  90.               {
  91.                /* Filename packet ! */
  92.                PacketSize = 128;
  93.                k = 0;
  94.                for(i=0;i<strlen(Filename);i++) Buffer[k++] = Filename[i];
  95.                Buffer[k++] = '\0';
  96.                sprintf(temp,"%ld",FileSize);
  97.                for(i=0;i<strlen(temp);i++) Buffer[k++] = temp[i];
  98.                while(k<128) Buffer[k++] = '\0';
  99.               }
  100.         else /* p > 0 */
  101.               {/* DATA Packet: use 1K or 128 byte block ? */
  102.                if(p<=Number1K) PacketSize = 1024;
  103.                else PacketSize = 128;
  104.                /* read next block from disk */
  105.                Code = read(Handle,Buffer,PacketSize);
  106.                if(Code<=0)
  107.                      {SayError(Port,"Error on disk read");
  108.                       return(FALSE);
  109.                      }
  110.                for(i=Code;i<PacketSize;i++) Buffer[i] = 0x1a;
  111.               }
  112.         /* send this packet */
  113.         if(!TxPacket(Port,p,PacketSize,Buffer,NCGchar)) return(FALSE);
  114.         SioDelay(5);
  115.         /* must 'restart' after non null packet 0 */
  116.         if(!EmptyFlag&&(p==0)) TxStartup(Port,&NCGchar);
  117.        } /* end -- for(p) */
  118.  /* done if empty packet 0 */
  119.  if(EmptyFlag)
  120.         {DisplayLine("Batch transfer complete",NULL,0);
  121.          return(TRUE);
  122.         }
  123.  /* all done. send EOT up to 10 times */
  124.  close(Handle);
  125.  if(!TxEOT(Port))
  126.      {SayError(Port,"EOT not acknowledged");
  127.       return(FALSE);
  128.      }
  129.  DisplayLine("Transfer Complete",NULL,0);
  130.  return(TRUE);
  131. } /* end -- TxyModem */
  132.  
  133. int RxyModem(
  134.    int Port,            /* COM port [0..3] */
  135.    char Filename[],     /* filename buffer */
  136.    char Buffer[],       /* 1024 byte data buffer */
  137.    char NCGchar,        /* NAK, 'C', or 'G' */
  138.    int BatchFlag)       /* if TRUE, get filename from packet 0 */
  139. {int i;
  140.  int Handle;         /* file Handle */
  141.  int p;              /* packet index */
  142.  int Code;           /* return code */
  143.  int FirstPacket;
  144.  char PacketNbr;
  145.  int PacketSize;           /* 128 or 1024 */
  146.  long FileSize;
  147.  char temp[81];
  148.  int  EOTflag = FALSE;
  149.  /* begin */
  150.  EOTflag = FALSE;
  151.  DisplayLine("XYMODEM Receive: Waiting for Sender ",NULL,0);
  152.  while(SioKeyPress()) SioKeyRead();
  153.  /* clear comm port */
  154.  SioRxFlush(Port);
  155.  /* Send NAKs, 'C's, or 'G's */
  156.  if(!RxStartup(Port,&NCGchar)) return(FALSE);
  157.  /* open file unless BatchFlag is on */
  158.  if(BatchFlag) FirstPacket = 0;
  159.  else
  160.      {/* start with packet 1 */
  161.       FirstPacket = 1;
  162.       /* open file passed in Filename[] for write */
  163.       Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  164.       if(Handle<0)
  165.           {strcpy(temp,"Cannot open ");
  166.            strcat(temp,Filename);
  167.            DisplayLine(temp,NULL,0);
  168.            return(FALSE);
  169.           }
  170.      }
  171.  /* get each packet in turn */
  172.  for(p=FirstPacket;;p++)
  173.      {/* user aborts ? */
  174.       if(SioKeyPress()) if((char)SioKeyRead()==CAN)
  175.         {TxCAN(Port);
  176.          return(FALSE);
  177.         }
  178.       /* issue message */
  179.       sprintf(temp,"Packet %d",p);
  180.       DisplayLine(temp,NULL,0);
  181.       /* get next packet */
  182.       if(!RxPacket(Port,p,&PacketSize,Buffer,NCGchar,&EOTflag)) return(FALSE);
  183.       if(p==0)
  184.         {/* copy Filename */
  185.          strcpy(Filename,Buffer);
  186.          /* done if null packet 0 */
  187.          if(Filename[0]=='\0')
  188.                 {DisplayLine("Batch Transfer Complete",NULL,0);
  189.                  return(TRUE);
  190.                 }
  191.         }
  192.       /* all done if EOT was received */
  193.       if(EOTflag)
  194.           {close(Handle);
  195.            DisplayLine("Transfer Complete",NULL,0);
  196.            return(TRUE);
  197.           }
  198.       /* process packet */
  199.       if(p==0)
  200.           {/* open file using filename in packet 0 */
  201.            Handle = open(Filename,O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,S_IWRITE);
  202.            if(Handle<0)
  203.                 {strcat(Buffer," -- open failed");
  204.                  DisplayLine(Buffer,NULL,0);
  205.                  return(FALSE);
  206.                 }
  207.            /* get file length */
  208.            FileSize = atol(&Buffer[1+strlen(Buffer)]);
  209.            /* must 'restart' after packet 0 */
  210.            RxStartup(Port,&NCGchar);
  211.           }
  212.       else /* DATA packet */
  213.           {/* write Buffer */
  214.            if(BatchFlag)
  215.                {if(FileSize<(long)PacketSize) i = (int) FileSize;
  216.                 else i = PacketSize;
  217.                 i = write(Handle,Buffer,i);
  218.                 FileSize -= (long)i;
  219.                }
  220.            else write(Handle,Buffer,PacketSize);
  221.           } /* end -- else */
  222.      } /* end -- for(p) */
  223. } /* end - RxyModem */
  224.  
  225. int TxCAN(int Port)
  226. {int i;
  227.  for(i=0;i<6;i++) PutChar(Port,CAN);
  228.  return(0);
  229. }